package com.tongzhou.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class MailSender {
	 private Properties properties; 
     private Session mailSession; 
     private MimeMessage mailMessage; 
     private Transport trans; 
     private MailSender() {}
     
     public static MailSender instance = null;
     
     public static MailSender getInstance(){
    	 if (instance == null){
    		 instance = new MailSender();
    	 }
    	 
    	 return instance;
     }
     
     public void sendMail(String userName, String toEmail, String pass) { 
         try { 
             properties = new Properties(); 
             //设置邮件服务器 
             properties.put("mail.smtp.host", Config.mail_smtp_host); 
             //验证 
             properties.put("mail.smtp.auth", "true"); 
             //根据属性新建一个邮件会话 
             mailSession = Session.getInstance(properties, 
                                               new Authenticator() { 
                 public PasswordAuthentication getPasswordAuthentication() { 
                     return new PasswordAuthentication(Config.server_mail, 
                    		 Config.server_pass); 
                 } 
             }); 
             mailSession.setDebug(true); 
             //建立消息对象 
             mailMessage = new MimeMessage(mailSession);
             //发件人 
             mailMessage.setFrom(new InternetAddress(Config.server_mail_addr)); 
             //收件人 
             mailMessage.setRecipient(MimeMessage.RecipientType.TO, 
                                 new InternetAddress(toEmail)); 
             //主题 
             mailMessage.setSubject(Config.subject, "UTF-8"); 
             //内容 
             StringBuffer sb = new StringBuffer();
             sb.append("<fieldset style=\"width=80%;heigth:90px:line-height:200%\"><legend>同舟科技</legend><br> &nbsp; ");
             sb.append("亲爱的").append(userName).append(", 您的密码是 <strong>").append(pass).append("</strong> 请妥善保管！<br><br></fieldset>");
             String text = new String(sb.toString().getBytes("UTF-8"), "UTF-8");
             System.out.println(text);
//             mailMessage.setDataHandler(new javax.activation.DataHandler(text,"text/html; charset=utf-8"));
//             mailMessage.setText(sb.toString()); 
             //发信时间 
             mailMessage.setSentDate(new Date());
             //添加附件
             Multipart mp = new MimeMultipart();
             MimeBodyPart mbp = new MimeBodyPart();
//             File attachement = new File("E:\\feedback.xls");
//             FileDataSource fds = new FileDataSource(attachement);
//             mbp.setDataHandler(new DataHandler(fds));
//             mbp.setFileName("feedback.xls");
//             mp.addBodyPart(mbp);
             
             
             mbp = new MimeBodyPart();
             mbp.setContent(text, "text/html; charset=utf-8");
             mp.addBodyPart(mbp);
             
             mailMessage.setContent(mp);
             //存储信息 
             mailMessage.saveChanges(); 
             // 
             trans = mailSession.getTransport("smtp");
             
             //发送 
             trans.send(mailMessage, mailMessage.getAllRecipients()); 
         } catch (Exception e) { 
             e.printStackTrace(); 
         } finally { 
         } 
     } 
     
     public static void main(String [] args){
    	 new MailSender().sendMail("gordon", "pengdingxiong@163.com", "123");
     }

}
